﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ExtraClub.Domain.Entities;
using System.Web.Security;
using ExtraClub.Domain;
using SubSonic;
using System.Transactions;
using ExtraClub.Domain.Framework;
using LB.Generics;

namespace ExtraClub.Domain {
    /// <summary>
    /// Summary description for form
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class BaseWS : System.Web.Services.WebService {
        protected int? EntityId = null;
        [WebMethod(true)]
        public string HelloWorld() {
            return "XTRA Club: Hellow World";
        }

        [WebMethod(true)]
        public string Process(Dictionary<string, object> data) {

            ECommand currentCommand = (ECommand)Enum.Parse(typeof(ECommand), data[Constants.FormField.Command].ToString(), true);
            EntityId = StringExt.IsInt32(data[Constants.FormField.EntityId].ToString());
            SiteContext.Current.SetLanguage(data[Constants.FormField.Language].ToString());

            if (currentCommand == ECommand.None) {
                return Filter(data);
            }

            string msg = "If you see this massage there occured some unexpected error on the server and your request was not prosessed!";

            using (TransactionScope ts = new TransactionScope()) {
                try {
                    WebUtils.CheckIfRequestAuthorized(data[Constants.FormField.PageUrl].ToString(), User);

                    switch (currentCommand) {
                        case ECommand.New:
                            Add(data);
                            break;
                        case ECommand.Edit:
                            Update(data);
                            break;
                        case ECommand.Delete:
                            Delete(data);
                            break;
                        default:
                            throw new Exception("Not correct commad.");
                    }

                    ts.Complete();
                    msg = WebUtils.GetMessage(data[Constants.FormField.ReturnUrl].ToString(), "form-callback-massage");
                } catch (Exception ex) {
                    msg = WebUtils.GetMessage(string.Format("Error:{0}. {1}", ex.Message, data[Constants.FormField.ReturnUrl].ToString()), "form-callback-error-massage");
                }
            }
            return msg;
        }

        protected virtual void Add(Dictionary<string, object> data) { ;}
        protected virtual void Update(Dictionary<string, object> data) { ;}
        protected virtual void Delete(Dictionary<string, object> data) { ;}
        protected virtual string Filter(Dictionary<string, object> data) { return null; }


        public void SetAddress(ref AddressRepository address, Dictionary<string, object> data) {
            address.CityRegion = data[AddressRepository.Columns.CityRegion.ToLower()].ToString();
            address.HouseNumber = data[AddressRepository.Columns.HouseNumber.ToLower()].ToString();
            address.Apartment = StringExt.IsByte(data[AddressRepository.Columns.Apartment.ToLower()]);
            address.Zip = StringExt.IsInt(data[AddressRepository.Columns.Zip.ToLower()]);

            object[] celular = (object[])data[AddressRepository.Columns.CellularPhone.ToLower()];
            address.CellularPhonePrefix = StringExt.IsByte(celular[0].ToString());
            address.CellularPhone = StringExt.IsLong(celular[1].ToString());

            object[] phone = (object[])data[AddressRepository.Columns.HomePhone.ToLower()];
            address.HomePhonePrefix = StringExt.IsByte(phone[0].ToString());
            address.HomePhone = StringExt.IsLong(phone[1].ToString());

            address.Email = data[AddressRepository.Columns.Email.ToLower()].ToString();
        }
    }
}

